/*推荐论文*/
function recommend() {
    var author = $('#author').val(),
        keyword = $('#keyword').val();
    $.get('/reference/recommend', {
        author: author,
        keyword: keyword
    }, function (data) {
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['420px', '240px'], //宽高
            content: 'html内容',
            success: function (data) {
                alert('Success');
            }
        });
    })
}

function test() {
    layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['750px', '430px'], //宽高
        content: 'html内容'
    });
}

/*弹出论文创建输入框*/
$('.aggregate-create').click(function () {
    $('.aggregate-create-input').fadeIn();
})

/*新建文献集-提交*/
$('#aggregate-create-submit').click(function () {
    var aggregateTitle = $('#aggregate-create-content').val();
    var aggregate = new Object();
    if (aggregateTitle == '') {
        layer.msg('输入为空！');
    } else {
        aggregate.aggregateTitle = aggregateTitle;
        aggregate.aggregateType = $('.my-tab-title li.layui-this').prev().text();
        $.ajax({
            url: '/reference/aggregate/insert',
            type: 'PUT',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify(aggregate),
            success: function (data) {
                if (data.data = true) {
                    window.location.href = "/reference/home";
                } else layer.msg('添加失败！');
            }
        })
    }
})

/*新建文献集-取消*/
$('#aggregate-create-cancel').click(function () {
    $('.aggregate-create-input').fadeOut();
});

/*切换文献列表*/
$('.aggregate-list-title').click(function () {
    var aggregateTitle = $(this).text(),
        aggregateId = $(this).prev().text();
    $('#aggregateId').text(aggregateId);
    $('#aggregateTitle').text(aggregateTitle);
    /*清除所有列表背景色*/
    $('.my-side-table tbody tr').removeClass('aggregate-list-hide');
    $('.my-side-table tbody tr').removeClass('aggregate-list-show');
    /*为选中列表添加背景色*/
    $($(this).parent()).addClass('aggregate-list-show');
    $('#docContent').val('');
    loadDocumentList(aggregateId);
});

/*文献集编辑*/
$('#aggregate-edit').click(function () {
    layer.prompt({
        formType: 2,
        value: $('#aggregateTitle').text(),
        title: '修改文献集名称',
    }, function (value, index, elem) {
        layer.close(index);
        updateAggregate(value);
    });
});

/*文献集下载*/
$('#aggregate-down').click(function () {
    var aggregateId = $('#aggregateId').text();
    window.location.href = '/reference/aggregate/download?aggregateId=' + aggregateId;
});

/*文献集删除*/
$('#aggregate-del').click(function () {
    var aggregateId = $('#aggregateId').text();
    $.ajax({
        url: "/reference/aggregate/delete",
        type: 'delete',
        data: {aggregateId: aggregateId},
        success: function (result) {
            if (result.data == true) {
                window.location.href = "/reference/home";
            } else layer.msg('删除失败!');
        }
    });
});


/*新增文献*/
$('#add-btn').click(function () {
    var docContent = $('#docContent').val(),
        aggregateId = $('#aggregateId').text();
    if (check(docContent)) {
        $.post('/reference/document/create',
            {docContent: docContent, aggregateId: aggregateId},
            function (data) {
                if (data.data = true) {
                    layer.msg('添加成功!');
                    loadDocumentList(aggregateId);
                    $('#docContent').val('');
                } else layer.msg('添加失败！');
            });
    }
});

/*重载文件列表*/
function loadDocumentList(aggregateId) {
    $.get('/reference/document/search/aggregateId', {aggregateId: aggregateId}, function (result) {
        var demoData = result.data;
        loadTable(demoData);
    });
}

/*修改文献*/
$('#edit-btn').click(function () {
    var docContent = $('#docContent').val(),
        docId = $('#docId').text(),
        aggregateId = $('#aggregateId').text();
    if (check(docContent)) {
        $.ajax({
            url: '/reference/document/update/content',
            type: 'put',
            data: {docContent: docContent, docId: docId},
            success: function () {
                loadDocumentList(aggregateId);
                layer.msg('修改成功！');
                $('#docContent').val('');
            }
        });
    }
});

function updateAggregate(title) {
    var aggregateId = $('#aggregateId').text();
    $.ajax({
        url: "/reference/aggregate/update/title",
        type: 'put',
        data: {aggregateTitle: title, aggregateId: aggregateId},
        success: function (result) {
            if (result.data == true) {
                /*设置论文标题*/
                $('#aggregateTitle').text(title);
                $('.my-side-table tbody tr.aggregate-list-show td:eq(2)').text(title);
                layer.msg('修改成功!');
            } else layer.msg('修改失败！');
        }
    })
}
